package com.source.lightfowl.basedata.service.impl;

import com.source.lightfowl.VO.SalesTerminalVO;
import com.source.lightfowl.basedata.dao.SalesTerminalDao;
import com.source.lightfowl.basedata.entity.SalesTerminal;
import com.source.lightfowl.basedata.service.SalesTerminalService;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.utils.StringTOArrayList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 13:06
 */
@Service
public class SalesTerminalServiceImpl implements SalesTerminalService {

    @Autowired
    private SalesTerminalDao salesTerminalDao;
    @Autowired
    private PersonDao personDao;
    @Override
    public List<SalesTerminalVO> findAll() {
        List<SalesTerminal> list = salesTerminalDao.findAll();
        List<SalesTerminalVO> collect = list.stream().map(s -> {
            SalesTerminalVO vo = new SalesTerminalVO();
            BeanUtils.copyProperties(s, vo);

//            Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getResponsible()).longValue()));
            Person user = personDao.findVO(s.getResponsible());
            vo.setUserName(user.getLoginName());

            return vo;
        }).collect(Collectors.toList());

        return collect;





    }

    @Override
    public Page<SalesTerminal> findAllPage(int page, int pageNum) {
        return salesTerminalDao.findAll(PageRequest.of(page-1,pageNum));
    }

    @Override
    public Optional<SalesTerminal> findById(Integer id) {
        return salesTerminalDao.findById(id);
    }

    @Override
    public void save(SalesTerminal sh) {
        if (sh.getCreateDate() == null){
            sh.setCreateDate(LocalDateTime.now());
        }
        salesTerminalDao.save(sh);
    }

    @Override
    public void deleteById(Integer id) {
        salesTerminalDao.deleteById(id);
    }

    @Override
    public void deleteBatch(String ids) {
        ArrayList<Integer> list = StringTOArrayList.toArrayList(ids);
        salesTerminalDao.deleteBatch(list);
    }
}
